package com.xcity.game.guild;

import com.xcity.db.entity.Player;
import com.xcity.pomelo.push.ChatPush;
import com.xcity.pomelo.push.SyncPush;
import com.xcity.pomelo.push.msg.DotTipPush;
import com.xcity.pomelo.push.sync.IntSync;

import gnu.trove.map.hash.TLongObjectHashMap;

/**
 * 公会频道
 * 但是也会利用id2player 发送一些不属于聊天的同步信息
 * @author shipeng.zhu
 */
public class GuildChannel {
	
	/**
	 * 同步时的状态
	 */
	public static int GUILD_DETAIL_CHANGE_SYNC = 1;//当公会信息更改时 发送广播
	public static int GUILD_MEMBER_CHANGE_SYNC = 1 << 1;//当公会成员更改时 发送广播
	public static int GUILD_APPLY_CHANGE_SYNC = 1 << 2;//当申请列表发生变化时 发送广播
	public static int GUILD_EVENT_CHANGE_SYNC = 1 << 3;//当公会事件发生变化时 发送广播
	
	private TLongObjectHashMap<Player> id2player;
	public GuildChannel(Player player){
		id2player = new TLongObjectHashMap<Player>();
		id2player.put(player.getId(), player);
	}
	public synchronized void broadcast(long srcId, ChatPush push){
		for(Player player : id2player.valueCollection()){
			if(player.isOnline()){
				player.send(push);
				if(srcId != player.getId()){
					player.send(DotTipPush.NEW_CHAT);
				}
			}
		}
	}
	
	public synchronized void join(Player player){
		if(!id2player.containsKey(player.getId())){
			id2player.put(player.getId(), player);
		}
	}
	
	public synchronized void leave(Player player){
		if(id2player.containsKey(player.getId())){
			id2player.remove(player.getId());
		}
	}
	
	public void sendSync(long srcId, IntSync push){
		for(Player player : id2player.valueCollection()){
			if(player.isOnline()){
				if(srcId != player.getId()){
					player.send(push);
				}
			}
		}
	}
}
